package com.qersoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qersoft.entity.ExamRule;
import com.qersoft.service.ExamRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exam/rule")
public class ExamRuleController {

    @Autowired
    private ExamRuleService examRuleService;

    // 添加规则
    @PostMapping("/add")
    public String add(@RequestBody ExamRule examRule) {
        if (examRuleService.add(examRule) > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    // 删除规则
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        if (examRuleService.delete(id)) {
            return "success";
        } else {
            return "fail";
        }
    }

    // 更新规则
    @PutMapping("/update")
    public String update(@RequestBody ExamRule examRule) {
        if (examRuleService.update(examRule)) {
            return "success";
        } else {
            return "fail";
        }
    }

    // 根据 ID 获取规则
    @GetMapping("/get/{id}")
    public ExamRule getById(@PathVariable Integer id) {
        return examRuleService.getById(id);
    }

    // 分页查询规则
    @GetMapping("/list")
    public IPage<ExamRule> list(@RequestParam(required = false) String name,
                                @RequestParam(defaultValue = "1") Integer pageNo,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        return examRuleService.list(name, pageNo, pageSize);
    }
}
